package jdbc;

import jdbc.common.InputField;
import jdbc.common.InputUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class LoginUserinfo {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("用户登录");
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        try(Connection connection = DBUtil.getConnection();) {
            String sql = "SELECT id,username,password,nickname,age "+
                    "FROM userinfo "+
                    "WHERE username = '"+username +"' AND password='"+password +"'";
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery(sql);
                if(rs.next()){  // 使用if而不是while，因为只需要判断是否存在匹配的用户
                    String nickname = rs.getString("nickname");
                    System.out.println("登录成功,欢迎["+nickname+"]回来");
                } else {
                    System.out.println("登录失败");
                }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
